import store from '../store'
import axios from 'axios'
let title = ''
let subTitle = ''
const exportURLArr = {
  '/reads/permissions/export': '阅读管理-阅读权限管理',
  '/feedbacks': '内容管理-意见反馈',
  '/marketing/prize-orders/export': '营销活动-实物订单列表',
  '/activities/collections-likes/export/curr':
    '拉新活动C端海报转介绍-集赞图审核'
}
const regURLArr = {
  '消息通知-导出数据': new RegExp(/^\/messages\/.+\/export/),
  '营销活动-邀请活动列表': new RegExp(
    /^\/activities\/summaries\/.+\/data-list/
  ),
  '营销活动-抽奖活动列表': new RegExp(/^\/activities\/lotteries\/.+/),
  '问卷调查-问卷列表': new RegExp(/^\/questionnaires\/.+\/export$/)
}
function regexp(url) {
  let findStatus = false
  let regURLValues = Object.values(regURLArr)
  let regURLArrKeys = Object.keys(regURLArr)
  regURLValues.forEach((item, index) => {
    if (item.test(url)) {
      findStatus = true
      title = regURLArrKeys[index].split('-')[0]
      subTitle = regURLArrKeys[index].split('-')[1]
    }
  })
  if (!findStatus && !!exportURLArr[url]) {
    title = exportURLArr[url].split('-')[0]
    subTitle = exportURLArr[url].split('-')[1]
  }
  return findStatus || !!exportURLArr[url]
}
async function requestPathReg(url, res) {
  if (res.config.responseType === 'blob') {
    let returnMsg = ''
    if (res.status === 409) {
      returnMsg = await res.data.text()
      returnMsg = JSON.parse(returnMsg).message
    }
    if (regexp(url)) {
      console.log(store.state.user)
      axios.post(`${process.env.VUE_APP_ZC_API}/of/log/add`, {
        businessType: 5,
        operatorType: 5,
        operParam: JSON.stringify(res.config.params || {}),
        operTime: new Date(),
        operUrl: url,
        status: res.status === 200 ? 0 : 1,
        userId: store.state.user.userId,
        userCode: store.state.user.dept.deptId,
        userName: store.state.user.phonenumber,
        nickname: store.state.user.userName,
        title,
        subTitle,
        operIp: returnCitySN['cip'],
        operLocation: returnCitySN['cname'],
        returnMsg
      })
    }
  }
}
export default requestPathReg
